#%%
import psutil
# cpu信息

print(psutil.cpu_times())
print(psutil.cpu_times().user)
print(psutil.cpu_times(percpu=True))
print(psutil.cpu_count())
print(psutil.cpu_count(logical=False))


#%%
# 内存信息
import psutil
print(psutil.virtual_memory())
print()
print(psutil.virtual_memory().total)
print(psutil.virtual_memory().free)
print(psutil.swap_memory())


#%%
#磁盘信息
import psutil

print(psutil.disk_partitions())
print()
print(psutil.disk_usage('/'))
print(psutil.disk_io_counters())
print(psutil.disk_io_counters(perdisk=True))



#%%
#网络信息
import psutil

temp = psutil.net_io_counters()
print(temp)
print()
print(psutil.net_io_counters(pernic=True))
print()


#%%
#其他系统信息
import psutil
import datetime

print(psutil.users())
print(psutil.boot_time())
print(datetime.datetime.fromtimestamp(psutil.boot_time()).strftime("%Y-%m-%d %H:%M:%S"))

#%%
#进程
import psutil

pid = psutil.pids()[-1]
p = psutil.Process(pid)
print(p.name(),p.exe())
print(p.cwd(),p.status(),p.create_time(),p.cpu_times(),p.cpu_affinity(),p.memory_percent(),p.memory_info(),p.io_counters(),p.connections(),p.num_threads())


#%%
# popen类的使用
import psutil
from subprocess import PIPE

p = psutil.Popen(["python", "-c", "print('hello,popen')"], stdout=PIPE)
print(p.name())
print(p.username())
print(p.communicate())



#%%
